package cn.com.hfuu.control;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import cn.com.hfuu.SystemUtil;
import cn.com.hfuu.UUIdUtil;
import cn.com.hfuu.ViewExcel;
import cn.com.hfuu.module.code;
import cn.com.hfuu.module.farmmedcine;
import cn.com.hfuu.module.operate;
import cn.com.hfuu.module.operategz;
import cn.com.hfuu.module.operatern;
import cn.com.hfuu.module.operatesq;
import cn.com.hfuu.module.qrcode;
import cn.com.hfuu.module.user;
import cn.com.hfuu.module.warehousetea;
import cn.com.hfuu.module.warehousetealog;
import cn.com.hfuu.module.warehouseteax;
import cn.com.hfuu.service.CodeService;
import cn.com.hfuu.service.GzService;
import cn.com.hfuu.service.OperateService;
import cn.com.hfuu.service.QrService;
import cn.com.hfuu.service.RnService;
import cn.com.hfuu.service.SqService;
import cn.com.hfuu.service.TeaService;
import cn.com.hfuu.service.TealogService;
import cn.com.hfuu.service.TeaxService;
import cn.com.hfuu.service.TeaxlogService;
import cn.com.hfuu.vo.Page;

@Controller
@RequestMapping("operate")
public class OperateControl {

	@Autowired
	OperateService operateservice;

	@Autowired
	CodeService codeservice;

	@Autowired
	TeaxService teaxservice;

	@Autowired
	SqService sqservice;

	@Autowired
	RnService rnservice;

	@Autowired
	GzService gzservice;

	@Autowired
	TealogService tealogService;

	@Autowired
	TeaxlogService teaxlogservice;

	@Autowired
	TeaService teaservice;

	@Autowired
	QrService qrservice;

	@RequestMapping("info")
	public String operate() {
		return "operate/op/oplist";
	}

	@RequestMapping("sqinfo")
	public String sqinfo() {
		return "operate/sq/operate";

	}

	@RequestMapping("rninfo")
	public String rninfo() {
		return "operate/rn/operate";

	}

	// public

	@RequestMapping("gzinfo")
	public String gzinfo() {
		return "operate/gz/operate";

	}

	@RequestMapping("oplist")
	@ResponseBody
	public Page opList(Integer start, Integer limit, operate op) {
		List<operate> list = operateservice.selectpage(start, limit,
				op.getOperateId(), -1);
		List<operate> total = operateservice.selectpagetotal(op.getOperateId(),
				-1);
		List<code> codelist = codeservice.selectbyType("110");
		List<code> codelist1 = codeservice.selectbyType("400");

		for (operate o : list) {
			o.setStatename("");
			if (o.getWarehouseState() != 3) {
				for (code c : codelist1) {
					if (o.getState() == c.getCodeValue()) {
						o.setCode(c);
						o.setStatename(c.getCodeName());
					}

				}
			}
			if (o.getState() == 1) {
				if (o.getWarehouseState() != 3) {
					o.setStatename(o.getStatename()+"(");
				}
				for (code c : codelist) {
					if (o.getWarehouseState() == c.getCodeValue()) {
						o.setCode(c);
						o.setStatename(o.getStatename()  + c.getCodeName());
					}

				}
				if (o.getWarehouseState() != 3) {
					o.setStatename(o.getStatename()+")");
				}
			}
			
			o.setWarehouseteax(teaxservice.selectbyid(o.getWarehouseTeaxid()));
			o.setWarehouseteaxlog(teaxlogservice.selectbyid(o
					.getWarehouseLogid()));
			System.out.println(o.toString());
		}

		Page page = new Page();
		page.setStart(start);
		page.setLimit(limit);
		page.setRoot(list);
		page.setTotal(total.size());
		return page;

	}

	@RequestMapping("inproc")
	@ResponseBody
	public String inproc(String id) {
		operate op = operateservice.selectbyid(id);
		op.setState(1);
		if (operateservice.update(op) > 0) {
			return "success";
		}
		return null;
	}

	@RequestMapping("sqlist")
	@ResponseBody
	public Page sqList(Integer start, Integer limit, operate op) {
		List<operate> list = operateservice.selectpage1(start, limit,
				op.getOperateId(), 0, 1);
		List<operate> total = operateservice.selectpagetotal1(
				op.getOperateId(), 0, 1);
		List<code> codelist = codeservice.selectbyType("110");
		for (operate o : list) {
			for (code c : codelist) {
				if (o.getWarehouseState() == c.getCodeValue())
					o.setCode(c);
			}
			o.setWarehouseteax(teaxservice.selectbyid(o.getWarehouseTeaxid()));
			o.setWarehouseteaxlog(teaxlogservice.selectbyid(o
					.getWarehouseLogid()));
			System.out.println(o.toString());
		}

		Page page = new Page();
		page.setStart(start);
		page.setLimit(limit);
		page.setRoot(list);
		page.setTotal(total.size());
		return page;

	}

	@RequestMapping("sqlistad")
	@ResponseBody
	public List<operate> sqListad() {

		List<operate> total = operateservice.selectpagetotal1(null, 0,1);
		for (operate operate : total) {
			operate.setWarehouseteax(teaxservice.selectbyid(operate
					.getWarehouseTeaxid()));
			operate.setWarehouseteaxlog(teaxlogservice.selectbyid(operate
					.getWarehouseLogid()));

		}

		return total;

	}

	@RequestMapping("rnlistad")
	@ResponseBody
	public List<operate> rnListad() {
		List<operate> total = operateservice.selectpagetotal(null, 1);
		for (operate o : total) {
			o.setWarehouseteax(teaxservice.selectbyid(o.getWarehouseTeaxid()));
			o.setWarehouseteaxlog(teaxlogservice.selectbyid(o
					.getWarehouseLogid()));
		}
		return total;

	}

	@RequestMapping("gzlistad")
	@ResponseBody
	public List<operate> gzListad() {

		List<operate> total = operateservice.selectpagetotal(null, 2);

		for (operate o : total) {
			o.setWarehouseteax(teaxservice.selectbyid(o.getWarehouseTeaxid()));
			o.setWarehouseteaxlog(teaxlogservice.selectbyid(o
					.getWarehouseLogid()));

		}

		return total;
	}

	@RequestMapping("rnlist")
	@ResponseBody
	public Page rnList(Integer start, Integer limit, operate op) {
		List<operate> list = operateservice.selectpage(start, limit,
				op.getOperateId(), 1);
		List<operate> total = operateservice.selectpagetotal(op.getOperateId(),
				1);
		List<code> codelist = codeservice.selectbyType("110");
		for (operate o : list) {
			for (code c : codelist) {
				if (o.getWarehouseState() == c.getCodeValue())
					o.setCode(c);
			}
			o.setWarehouseteax(teaxservice.selectbyid(o.getWarehouseTeaxid()));
			o.setWarehouseteaxlog(teaxlogservice.selectbyid(o
					.getWarehouseLogid()));

		}
		Page page = new Page();
		page.setStart(start);
		page.setLimit(limit);
		page.setRoot(list);
		page.setTotal(total.size());
		return page;

	}

	@RequestMapping("gzlist")
	@ResponseBody
	public Page gzList(Integer start, Integer limit, operate op) {
		List<operate> list = operateservice.selectpage(start, limit,
				op.getOperateId(), 2);
		List<operate> total = operateservice.selectpagetotal(op.getOperateId(),
				2);
		List<code> codelist = codeservice.selectbyType("110");
		for (operate o : list) {
			for (code c : codelist) {
				if (o.getWarehouseState() == c.getCodeValue())
					o.setCode(c);
			}

			o.setWarehouseteax(teaxservice.selectbyid(o.getWarehouseTeaxid()));
			o.setWarehouseteaxlog(teaxlogservice.selectbyid(o
					.getWarehouseLogid()));

		}
		Page page = new Page();
		page.setStart(start);
		page.setLimit(limit);
		page.setRoot(list);
		page.setTotal(total.size());
		return page;
	}

	@RequestMapping("sq")
	public String sq(Model m, String id) {
		operate op = operateservice.selectbyid(id);
		m.addAttribute("operate", op);
		warehouseteax teax = teaxservice.selectbyid(op.getWarehouseTeaxid());
		m.addAttribute("teax", teax);
		/*
		 * List<warehouseteax> warehouseteax=teaxservice.selectbyoperate();
		 * m.addAttribute("teax", warehouseteax);
		 */
		return "operate/sq/addsq";

	}

	@RequestMapping("rn")
	public String rn(Model m, String id) {
		operate op = operateservice.selectbyid(id);
		m.addAttribute("operate", op);
		warehouseteax teax = teaxservice.selectbyid(op.getWarehouseTeaxid());
		m.addAttribute("teax", teax);
		return "operate/rn/addrn";

	}

	@RequestMapping("gz")
	public String gz(Model m, String id) {
		operate op = operateservice.selectbyid(id);
		m.addAttribute("operate", op);
		warehouseteax teax = teaxservice.selectbyid(op.getWarehouseTeaxid());
		m.addAttribute("teax", teax);
		return "operate/gz/addgz";

	}

	@RequestMapping("sqproc")
	@ResponseBody
	public String addsq(operatesq sq, HttpServletRequest req) {
		HttpSession s = req.getSession();
		user u = (user) s.getAttribute("user");
		UUIdUtil idu = new UUIdUtil();
		SystemUtil sys = new SystemUtil();
		sq.setOperateUserid(u.getUserId());
		sq.setOperateSqid(idu.getUuid());
		sq.setOperateTime(sys.getDatetime());

		operate op = operateservice.selectbyid(sq.getOperateId());
		sq.setOperateTeaxid(op.getWarehouseTeaxid());
		if (sqservice.insert(sq) > 0) {
			op.setWarehouseState(1);
			operateservice.update(op);
			return "success";
		}
		return null;

	}

	@RequestMapping("sqprocad")
	@ResponseBody
	public String addsqad(@RequestBody operatesq sq, HttpServletRequest req) {
		HttpSession s = req.getSession();
		user u = (user) s.getAttribute("user");
		UUIdUtil idu = new UUIdUtil();
		SystemUtil sys = new SystemUtil();
		sq.setOperateUserid(u.getUserId());
		sq.setOperateSqid(idu.getUuid());
		sq.setOperateTime(sys.getDatetime());

		operate op = operateservice.selectbyid(sq.getOperateId());
		sq.setOperateTeaxid(op.getWarehouseTeaxid());
		if (sqservice.insert(sq) > 0) {
			op.setWarehouseState(1);
			operateservice.update(op);
			return "success";
		}
		return null;

	}

	@RequestMapping("rnproc")
	@ResponseBody
	public String addrn(operatern rn, HttpServletRequest req) {
		HttpSession s = req.getSession();
		user u = (user) s.getAttribute("user");
		UUIdUtil idu = new UUIdUtil();
		SystemUtil sys = new SystemUtil();
		rn.setOperateUserid(u.getUserId());
		rn.setOperateRnid(idu.getUuid());
		rn.setOperateTime(sys.getDatetime());
		operate op = operateservice.selectbyid(rn.getOperateId());
		rn.setOperateTeaxid(op.getWarehouseTeaxid());
		if (rnservice.insert(rn) > 0) {
			op.setWarehouseState(2);
			operateservice.update(op);
			return "success";
		}
		return null;

	}

	@RequestMapping("rnprocad")
	@ResponseBody
	public String addsqad(@RequestBody operatern rn, HttpServletRequest req) {
		HttpSession s = req.getSession();
		user u = (user) s.getAttribute("user");
		UUIdUtil idu = new UUIdUtil();
		SystemUtil sys = new SystemUtil();
		rn.setOperateUserid(u.getUserId());
		rn.setOperateRnid(idu.getUuid());
		rn.setOperateTime(sys.getDatetime());
		operate op = operateservice.selectbyid(rn.getOperateId());
		rn.setOperateTeaxid(op.getWarehouseTeaxid());
		if (rnservice.insert(rn) > 0) {
			op.setWarehouseState(2);
			operateservice.update(op);
			return "success";
		}
		return null;

	}

	/**
	 * 干燥完成后入库 产品入库 日志表
	 * 
	 * @param sq
	 * @param req
	 * @return
	 */
	@RequestMapping("gzproc")
	@ResponseBody
	public String addgz(operategz gz, HttpServletRequest req) {
		HttpSession s = req.getSession();
		user u = (user) s.getAttribute("user");
		UUIdUtil idu = new UUIdUtil();
		SystemUtil sys = new SystemUtil();
		gz.setOperateUserid(u.getUserId());
		gz.setOperateGzid(idu.getUuid());
		gz.setOperateTime(sys.getDatetime());
		operate op = operateservice.selectbyid(gz.getOperateId());
		gz.setOperateTeaxid(op.getWarehouseTeaxid());

		if (gzservice.insert(gz) > 0) {
			op.setWarehouseState(3);
			operateservice.update(op);
			// 产品入库
			warehousetea tea = new warehousetea();
			tea.setWarehouseTeaid(idu.getUuid());
			tea.setTeaQuality(gz.getOperateQuality());
			tea.setTeaName(teaxservice.selectbyid(op.getWarehouseTeaxid())
					.getWarehouseTeaname());
			tea.setTeaxId(gz.getOperateTeaxid());
			tea.setState(0);
			tea.setOperateid(op.getOperateId());
			teaservice.insert(tea);
			// 日志记录产品入库
			/*
			 * warehousetealog tealog=new warehousetealog();
			 * tealog.setWarehouseTealogid(idu.getUuid());
			 * tealog.setWarehouseTeaquality(gz.getOperateQuality());
			 * tealog.setWarehouseLoguserid(u.getUserId());
			 * tealog.setWarehouseTealogtime(sys.getDatetime());
			 * tealog.setWarehouseTeatype(1);
			 * tealog.setWarehouseTeaid(tea.getWarehouseTeaid());
			 * tealogService.insert(tealog);
			 */
			// 二维码
			/*
			 * qrcode qr=new qrcode(); qr.setQrId(idu.getUuid());
			 * qr.setTeaId(tea.getWarehouseTeaid());
			 * qr.setTeaxId(gz.getOperateTeaxid()); qr.setCount(0);
			 * qr.setOperateId(op.getOperateId()); warehouseteax
			 * teax=teaxservice.selectbyid(gz.getOperateTeaxid());
			 * qr.setPickId(teax.getWarehousePickid()); qrservice.insert(qr);
			 */

			return "success";
		}
		return null;

	}

	@RequestMapping("gzprocad")
	@ResponseBody
	public String addgzad(@RequestBody operategz gz, HttpServletRequest req) {
		HttpSession s = req.getSession();
		user u = (user) s.getAttribute("user");
		UUIdUtil idu = new UUIdUtil();
		SystemUtil sys = new SystemUtil();
		gz.setOperateUserid(u.getUserId());
		gz.setOperateGzid(idu.getUuid());
		gz.setOperateTime(sys.getDatetime());
		operate op = operateservice.selectbyid(gz.getOperateId());
		gz.setOperateTeaxid(op.getWarehouseTeaxid());

		if (gzservice.insert(gz) > 0) {
			op.setWarehouseState(3);
			op.setState(2);
			operateservice.update(op);
			// 产品入库
			warehousetea tea = new warehousetea();
			tea.setWarehouseTeaid(idu.getUuid());
			tea.setTeaQuality(gz.getOperateQuality());
			tea.setTeaName(teaxservice.selectbyid(op.getWarehouseTeaxid())
					.getWarehouseTeaname());
			tea.setTeaxId(gz.getOperateTeaxid());
			tea.setOperateid(op.getOperateId());
			tea.setState(0);
			teaservice.insert(tea);
			/*
			 * //日志记录产品入库 warehousetealog tealog=new warehousetealog();
			 * tealog.setWarehouseTealogid(idu.getUuid());
			 * tealog.setWarehouseTeaquality(gz.getOperateQuality());
			 * tealog.setWarehouseLoguserid(u.getUserId());
			 * tealog.setWarehouseTealogtime(sys.getDatetime());
			 * tealog.setWarehouseTeatype(1);
			 * tealog.setWarehouseTeaid(tea.getWarehouseTeaid());
			 * tealogService.insert(tealog); //二维码 qrcode qr=new qrcode();
			 * qr.setQrId(idu.getUuid()); qr.setTeaId(tea.getWarehouseTeaid());
			 * qr.setTeaxId(gz.getOperateTeaxid()); qr.setCount(0);
			 * qr.setOperateId(op.getOperateId()); warehouseteax
			 * teax=teaxservice.selectbyid(gz.getOperateTeaxid());
			 * qr.setPickId(teax.getWarehousePickid()); qrservice.insert(qr);
			 */

			return "success";
		}
		return null;

	}
	
	@RequestMapping("export")
	public ModelAndView export(ModelMap map) {
		List<operate> list = operateservice.selectpagetotal(null,
				-1);
		List<code> codelist = codeservice.selectbyType("110");
		List<code> codelist1 = codeservice.selectbyType("400");
		List<Map<String, String>> mapList = new ArrayList<Map<String, String>>();
		for (operate o : list) { 
			Map<String, String> map1 = new HashMap<String, String>();
			o.setStatename("");
			if (o.getWarehouseState() != 3) {
				for (code c : codelist1) {
					if (o.getState() == c.getCodeValue()) {
						o.setCode(c);
						o.setStatename(c.getCodeName());
					}

				}
			}
			if (o.getState() == 1) {
				if (o.getWarehouseState() != 3) {
					o.setStatename(o.getStatename()+"(");
				}
				for (code c : codelist) {
					if (o.getWarehouseState() == c.getCodeValue()) {
						o.setCode(c);
						o.setStatename(o.getStatename()  + c.getCodeName());
					}
				}
				if (o.getWarehouseState() != 3) {
					o.setStatename(o.getStatename()+")");
				}
			}
			o.setWarehouseteax(teaxservice.selectbyid(o.getWarehouseTeaxid()));
			o.setWarehouseteaxlog(teaxlogservice.selectbyid(o
					.getWarehouseLogid()));
			System.out.println(o.toString());
			map1.put("编号",o.getOperateId());
			map1.put("茶名",o.getWarehouseteax().getWarehouseTeaname());
			map1.put("重量(克)",""+o.getWarehouseteaxlog().getWarehouseTeaxquality());
			map1.put("状态", o.getStatename());
			map1.put("出库时间",o.getWarehouseteaxlog().getWarehouseTeaxlogtime());		 
			mapList.add(map1);

		}
		String[] title = { "编号","茶名", "重量(克)","状态","出库时间" };
		map.addAttribute("excelList", mapList);
		ViewExcel ve = new ViewExcel(title, "茶叶加工信息");
		return new ModelAndView(ve, map);
	}

}
